package com.example.ddz.framework.impl;

/**
 * Created by IntelliJ IDEA.
 * User: Marcel Melzig
 * Date: 31.05.11
 * Time: 09:55
 */

/**
 * @author Marcel Melzig
 * @version 0.0.1
 */

import com.example.ddz.Assets;
import com.example.ddz.framework.IGame;
import com.example.ddz.framework.IGraphics;
import com.example.ddz.framework.IInput;

/**
 * The screen implements the methods to run the game.
 */
public abstract class Screen
{
    protected final IGame game;
    protected int charWidth;
    protected int dotWidth;
    protected int dotScrX;

    /**
     * Creates an instance of the Screen class. The screen gets an object of the type IGame as a parameter. That way the
     * screen has the ability to use the underlying methods to display graphics or play sounds.
     *
     * @param game Encapsulates the game main methods.
     */
    protected Screen(IGame game)
    {
        this.game = game;
    }

    /**
     * Updates the screen.
     *
     * @param deltaTime The time between the last update of the framebuffer and the current frame.
     */
    public abstract void Update(float deltaTime);

    /**
     * Presents this screen on the display.
     *
     * @param deltaTime The time between the last update of the framebuffer and the current frame.
     */
    public abstract void Present(float deltaTime);

    /**
     * Pauses the game.
     */
    public abstract void Pause();

    /**
     * Resumes the game from pause mode.
     */
    public abstract void Resume();

    /**
     * Disposes the instance.
     */
    public abstract void Dispose();


    /**
     * Checks if the coordinates of the touch event are in the range of the defined rectangle.
     *
     * @param event  The touch event.
     * @param x      The starting x coordinate of the defined rectangle.
     * @param y      The starting y coordinate of the defined rectangle.
     * @param width  The width of the defined rectangle.
     * @param height The height of the defined rectangle.
     * @return True if the coordinates of the given touch event are in the defined rectangle, otherwise false.
     */
    protected static boolean InBounds(IInput.TouchEvent event, int x, int y, int width, int height)
    {
        return event.x > x && event.x < x + width - 1 && event.y > y && event.y < y + height - 1;
    }

    /**
     * Draws a digit or the point of the numbers.png image to screen to display the highscores.
     *
     * @param g    An IGraphics instance used to draw the number onto the screen.
     * @param line The highscore value as a string.
     * @param x    The x coordinate the digit should be drawn.
     * @param y    The y coordinate the digit should be drawn.
     */
    protected void DrawText(IGraphics g, String line, int x, int y)
    {
        int len = line.length();

        for (int i = 0; i < len; i++)
        {
            char character = line.charAt(i);

            if (character == ' ')
            {
                x += this.charWidth;
                continue;
            }

            int srcX;
            int srcWidth;

            if (character == '.')
            {
                srcX = this.dotScrX;
                srcWidth = this.dotWidth;
            }
            else
            {
                srcX = (character - '0') * this.charWidth;
                srcWidth = this.charWidth;
            }

            g.DrawPixmap(Assets.numbers, x, y, srcX, 0, srcWidth, Assets.numbers.GetHeight());
            x += srcWidth;
        }
    }
}
